package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.volley.VolleySingleton;


public class LargeImageActivity extends Activity implements View.OnClickListener {

    private ImageView largeImage;
    private String images;
    private Button saveImage;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_large);

        largeImage = (ImageView) findViewById(R.id.largeImage);
        largeImage.setOnClickListener(this);
        saveImage = (Button) findViewById(R.id.saveImage);
        saveImage.setOnClickListener(this);

        //获得传过来的图片地址
        Intent intent = getIntent();
        images = intent.getStringExtra("image");
        getBitmap(images, largeImage);

    }

    private void getBitmap(String url, final ImageView imageView){

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 10000, 10000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(imageRequest);

    }

    @Override
     public void onClick(View v) {
        switch (v.getId()){
            case R.id.largeImage:
                this.finish();
                break;
            case R.id.saveImage:
                saveImage(images);
                break;
        }
    }
    /***
     * 保存图片的方法
     * **/
    private void saveImage(String url){

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                //将图片保存到手机
                MediaStore.Images.Media.insertImage(getContentResolver(), response, "id", "id");
                Toast.makeText(LargeImageActivity.this, " 图片保存成功! 快去看看吧..", Toast.LENGTH_LONG).show();
            }
        }, 10000, 10000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(imageRequest);
    }







}
